package com.yun.datastructure.disjoinset;

import java.util.Arrays;

/**
 * 并查集
 */
public class DisjointSet {

    public static void main(String[] args) {
        DisjointSet set=new DisjointSet(7);
        System.out.println(set);
        //索引对应顶点
        //元素用来表示与之有关系的顶点
    }
    int[] s;

    public DisjointSet(int size) {
        s = new int[size];
        for (int i = 0; i < size; i++) {
            s[i] = i;
        }
    }

    public int find(int x) {
        if(x==s[x]){
            return x;
        }
        return s[x]=find(s[x]);
    }

    //union是让两个集合相交，即选出新老大，x和y是原老大索引
    public void union(int x, int y) {
        s[y]=x;
    }

    @Override
    public String toString() {
        return Arrays.toString(s);
    }
}
